﻿namespace Z2
{

	using Microsoft.Xna.Framework;
	using Microsoft.Xna.Framework.Graphics;
	using Microsoft.Xna.Framework.Input;
	using Z2.Language;
	using Z2.Core;
	using System.Diagnostics;
	using System;
	using System.Collections.Generic;

	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public class Zelvici2 : Game
	{

		#region Members

		private RenderTarget2D m_lightBuffer;
		private Color m_globalLight = new Color(0.8f, 0.8f, 0.8f, 1.0f);
		private Rectangle m_screen;

		private GraphicsDeviceManager m_graphics;
		private SpriteBatch m_spriteBatch;

		private GameCursor m_cursor;
		private Player m_player;
		private Camera m_camera;
		private Hud m_hud;
		private World m_world;

		private string m_runMap;

		private Texture2D m_light;

		#endregion

		#region Constructor

		/// <summary>
		/// Konstruktor
		/// </summary>
		/// <param name="runMap">Pokud obsahuje název mapy, spustí se tato mapa.</param>
		public Zelvici2(string runMap)
			: base()
		{
			this.m_runMap = runMap;

			this.m_graphics = new GraphicsDeviceManager(this);
			this.m_graphics.PreferredBackBufferWidth = 1024; // Properties.Settings.Default.Width;
			this.m_graphics.PreferredBackBufferHeight = 600; // Properties.Settings.Default.Height;
			//this.m_graphics.IsFullScreen = true; // Properties.Settings.Default.Fullscreen;
			this.m_graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(this.Graphics_PreparingDeviceSettings);
		}

		#endregion

		#region Override Methods

		protected override void Initialize()
		{
			this.Window.Title = Strings.WindowTitle;
			SpriteManager.Instance.Initialize(this.Content, this.m_graphics);
			this.m_camera = new Camera(this.m_graphics);
			this.m_lightBuffer = new RenderTarget2D(this.GraphicsDevice,
				this.m_graphics.PreferredBackBufferWidth,
				this.m_graphics.PreferredBackBufferHeight, 1,
				SurfaceFormat.Color);
			this.m_screen = new Rectangle(0, 0, this.m_graphics.PreferredBackBufferWidth, this.m_graphics.PreferredBackBufferHeight);

			base.Initialize();

		}

		protected override void LoadContent()
		{
			this.m_light = this.Content.Load<Texture2D>("Graphics/light");

			this.m_spriteBatch = new SpriteBatch(this.GraphicsDevice);

			this.m_cursor = new GameCursor();

			this.m_player = new Player();
			
			this.m_world = new World(string.IsNullOrEmpty(this.m_runMap)? "blah" : this.m_runMap);
			this.m_world.Load();

			this.m_world.RegisterObject(this.m_player);
			this.m_camera.Follow = this.m_player;
			this.m_hud = new Hud(this.m_graphics);
		}
		

		protected override void UnloadContent()
		{
			this.Content.Unload();
			SpriteManager.Instance.Dispose();
			base.UnloadContent();
		}

		protected override void Update(GameTime gameTime)
		{
			KeyboardState keys = Keyboard.GetState();
			

			this.m_player.Update();

			this.m_cursor.Update();

			base.Update(gameTime);
		}

		private void BeginDrawLight()
		{
			this.GraphicsDevice.SetRenderTarget(0, this.m_lightBuffer);
			this.GraphicsDevice.Clear(this.m_globalLight);
			this.m_spriteBatch.Begin(SpriteBlendMode.Additive);
			Rectangle r = new Rectangle(
				(int)(this.m_player.Position.X + Camera.Instance.Position.X - 86.0f),
				(int)(this.m_player.Position.Y + Camera.Instance.Position.Y - 109.0f), 300, 300);
			m_spriteBatch.Draw(this.m_light, r, Color.White);
			m_spriteBatch.End();
			this.GraphicsDevice.SetRenderTarget(0, null);
		}

		private void EndDrawLight()
		{
			this.m_spriteBatch.Begin(SpriteBlendMode.Additive, SpriteSortMode.Immediate, SaveStateMode.None);
			this.GraphicsDevice.RenderState.SourceBlend = Blend.Zero;
			this.GraphicsDevice.RenderState.DestinationBlend = Blend.SourceColor;
			this.m_spriteBatch.Draw(this.m_lightBuffer.GetTexture(), this.m_screen, Color.White);
			this.m_spriteBatch.End();
		}

		protected override void Draw(GameTime gameTime)
		{
			this.BeginDrawLight();

			this.m_spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
			this.m_world.Draw(this.m_spriteBatch);
			this.m_spriteBatch.End();
			this.EndDrawLight();

			this.m_spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
			this.m_hud.Draw(this.m_spriteBatch);
			this.m_cursor.Draw(this.m_spriteBatch);
			this.m_spriteBatch.End();

			base.Draw(gameTime);
		}

		#endregion

		#region Event Handling

		void Graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
		{
			PresentationParameters pp = e.GraphicsDeviceInformation.PresentationParameters;
#if XBOX
			pp.MultiSampleQuality = 0;
			pp.MultiSampleType = MultiSampleType.FourSamples;
#else
			// TODO: Načíst nastavení
#endif
		}

		#endregion

	}
}
